﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Radar
{
    public partial class Form1 : Form
    {
        Radar radar;
        int speed = 200;
        MucTieu mb;
        List<ToaDo> qd;
        System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
       // public static List<MucTieu> listTarget = new List<MucTieu>();
        public Form1()
        {
            InitializeComponent();
            t.Interval = 500;
            t.Tick += new EventHandler(t_Tick);
            t.Enabled = true;
        }


        void t_Tick(object sender, EventArgs e)
        {
            double max_angle = radar.maxAngle * 0.0174532925d;

            if (listTarget.Count == 0)
            {
                //lvTarget.Items.Clear();
                //cbTarget.Items.Clear();
                //cbTarget.Text = "";
            }
            else
            {
                //lvTarget.Items.Clear();
                //cbTarget.Items.Clear();
                // select which of the three items to update
                foreach (MucTieu item in listTarget)
                {

                    //khoang cach di duoc cua muc tieu trong 1 s
                    double s = item._current.velocity * 1000 / (3600000 * 3);

                    //toa do tuong doi giua diem di va diem den
                    double x = item._to.X - item._current.X;
                    double y = item._to.Y - item._current.Y;
                    double chenhlech_kc = Math.Sqrt(x * x + y * y);
                    double gocngang = Math.Acos(x / chenhlech_kc);
                    //doi sang do.
                    gocngang = gocngang / 0.0174532925d;

                    if (y < 0)
                    {
                        gocngang = 360d - gocngang;
                    }
                    //doi sang radian de tinh sin va cos
                    gocngang *= 0.0174532925d;
                    double z = item._to.Z - item._current.Z;
                    double gocdung = Math.Asin(z / Math.Sqrt(z * z + chenhlech_kc * chenhlech_kc));
                    //toa do muc tieu sau 1s
                    double z1 = item._current.Z + (s * Math.Sin(gocdung));
                    double chenhlech_kc1 = (float)(s * Math.Abs(Math.Cos(gocdung)));

                    double x1 = item._current.X + chenhlech_kc1 * Math.Cos(gocngang);
                    double y1 = item._current.Y + chenhlech_kc1 * Math.Sin(gocngang);
                    ToaDo current = new ToaDo(item._current.stt, x1, y1, z1, item._current.velocity, "abc");
                    //gan diem nay thanh diem bat dau
                    item._current = current;
                    //_radar.AddItem(item);

                    //_radar.ImageUpdate += new ImageUpdateHandler(_radar_ImageUpdate);
                    if (radar._az + 20 > RadaA1.getAzimuth(current) && radar._az - 20 < RadaA1.getAzimuth(current) && RadaA1.getRange(current) <= pictureBox1.Width / 2)// && (Radar.getRange(item._current) * Math.Sin(max_angle)) > item._current.Z)
                    {


                        bool Flag = true;
                        for (int j = 0; j < lvTarget.Items.Count; j++)
                        {

                            if (lvTarget.Items[j].Text.Trim() == item.ID.ToString().Trim())
                            {
                                ListViewItem lvItem = lvTarget.Items[j];
                                lvItem.Text = item.ID.ToString();
                                lvItem.SubItems[1].Text = (item._category);
                                lvItem.SubItems[2].Text = (RadaA1.getAzimuth(item._current).ToString());

                                int rg = (int)RadaA1.getRange(item._current);

                                lvItem.SubItems[3].Text = (rg.ToString());
                                lvItem.SubItems[4].Text = (item._current.Z.ToString());
                                lvItem.SubItems[5].Text = (item._current.velocity.ToString());
                                Flag = false;
                                /*txttoadoY.Text = Math.Round(item._current.Y).ToString();
                                txttoaDoX.Text = Math.Round(item._current.X).ToString();
                                txtdocao.Text = Math.Round(item._current.Z).ToString();
                                txtvantoc.Text = (item._current.velocity.ToString());
                                txthuongtoi.Text = Convert.ToInt16(getAzimuth(Convert.ToInt16(item._to.X), Convert.ToInt16(item._to.Y), pictureBox1.Width)).ToString();
                                vitri.Text = rg.ToString();*/
                                radar.AddItem(item);
                                //_radar.ImageUpdate += new ImageUpdateHandler(_radar_ImageUpdate);
                            }
                        }
                        if (Flag)
                        {
                            ListViewItem lvItem = new ListViewItem();
                            lvItem.Text = item.ID.ToString();
                            lvItem.SubItems.Add(item._category);
                            lvItem.SubItems.Add(RadaA1.getAzimuth(item._current).ToString());

                            int rg = (int)RadaA1.getRange(item._current);

                            lvItem.SubItems.Add(rg.ToString());
                            lvItem.SubItems.Add(item._current.Z.ToString());
                            lvItem.SubItems.Add(item._current.velocity.ToString());
                            lvItem.BackColor = item._color;
                            lvTarget.Items.Add(lvItem);
                        }
                    }
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            radar = new RadaA1(pictureBox1.Width, 344, 50, 285, 3);
           // radar.Sovongtron = 3;
            pictureBox1.Image = radar.Image;
            radar.pathIcon = @"E:\newProject\RadarA\RadarA\RadarA\Icon\maybay.png";
            radar.ImageUpdate += new ImageUpdateHandler(_radar_ImageUpdate);
            radar.DrawScanInterval = speed / 20;
            RadaA1.TatmoAnten = false;
        }

        private void _radar_ImageUpdate(object sender, ImageUpdateEventArgs e)
        {
            pictureBox1.Image = e.Image; 
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void pictureBox1_MouseMove_1(object sender, MouseEventArgs e)
        {
           // toado.Text = "Cự ly: " + e.Location.X.ToString() + " Phương vị " + e.Location.Y.ToString();
           int abc = checkmousemove(e.Location.X, e.Location.Y, pictureBox1.Width);


            if (abc <= 240)
            {
                int ab = getAzimuth(e.Location.X, e.Location.Y, pictureBox1.Width);
                toado.Text = "Cự ly: " + e.Location.X.ToString() + " Phương vị " + e.Location.Y.ToString();
                lblPhuongVi.Text = "hướng: " + ab.ToString();
                lblCuLy.Text = "Db:"+Convert.ToInt16(getRange(e.Location.X, e.Location.Y, pictureBox1.Width)).ToString();
            }
        }

        private int getAzimuth(int _x, int _y, int size)
        {
            double r, x, y;
            r = size * 0.5d;//ban kinh duong tron
            //xac dinh toa do so voi tam
            x = _x - r;
            y = _y - r;
            double canh_huyen = Math.Sqrt(x * x + y * y);
            //tinh goc so voi truc x
            double angle = Math.Acos(x / canh_huyen);
            //doi sang do.
            angle = angle / 0.0174532925d;

            if (y < 0)
            {
                angle = 360d - angle;
            }
            //chuyen sang goc so voi truc. y - Phuong BAC'

            double azimuth = angle - 270d;
            if (azimuth < 0) azimuth = azimuth + 360d;
            return (int)azimuth;
        }
        public PointF huong(int azimuth, int widthPictrueBox)
        {
            int _size = widthPictrueBox;
            //chuyen goc phuong vi sang goc theo truc X
            double angle = (270d + (double)azimuth);

            // chuyen sang radian
            angle *= 0.0174532925d;

            double r, x, y;

            // tinh x va y theo size cua man hinh scan radar co san
            r = _size * 0.5;

            //_size = pictureBox1.Width;

            x = (((double)_size * 0.5d) + (r * Math.Cos(angle)));
            y = (((double)_size * 0.5d) + (r * Math.Sin(angle)));

            return new PointF((float)x, (float)y);
        }

        private int checkmousemove(int _x, int _y, int size)
        {
            double r, x, y;
            r = size * 0.5d;//ban kinh duong tron
            //xac dinh toa do so voi tam
            x = _x - r;
            y = _y - r;
            double canh_huyen = Math.Sqrt(x * x + y * y);
            return Convert.ToInt16(canh_huyen);
        }

        private void butMoQuayAnten08_Click(object sender, EventArgs e)
        {
            RadaA1.TatmoAnten = true;
        }

        private void butTatQuayAnten09_Click(object sender, EventArgs e)
        {
            RadaA1.TatmoAnten = false;
        }

        private void button17_Click(object sender, EventArgs e)
        {
            radar.DrawScanInterval = 40;
        }

        private void button22_Click(object sender, EventArgs e)
        {
            //5 giây
            radar.DrawScanInterval = speed / 20;
        }

        private void button23_Click(object sender, EventArgs e)
        {
            //10 giây
            radar.DrawScanInterval = speed / 20;
        }

        private void button28_Click(object sender, EventArgs e)
        {
            //10 giay
            radar.DrawScanInterval = speed / 20;
        }

        private void button27_Click(object sender, EventArgs e)
        {
            //20 giay
            radar.DrawScanInterval = speed / 10;
        }
        //hàm tính vị trí so với tâm
        public static double getRange(int _x, int _y, int size)
        {
            double r, x, y;
            r = size * 0.5d;//ban kinh duong tron
            //xac dinh toa do so voi tam
            x = _x - r;
            y = _y - r;
            double canh_huyen = Math.Sqrt(x * x + y * y);
            return Convert.ToInt16(canh_huyen)/8;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            button4.Enabled = false;
            int Dkm = Convert.ToInt16(numericUpDown2.Value);
            int Bdo = Convert.ToInt16(numericUpDown3.Value);
            int Hkm = Convert.ToInt16(numericUpDown6.Value);
            int GocT = Convert.ToInt16(numericUpDown7.Value);
            int Vvt = Convert.ToInt16(numericUpDown8.Value);

            PointF huongBdo = huong(Bdo, pictureBox1.Width);
            PointF huongGocT = huong(GocT, pictureBox1.Width);


            ToaDo td1 = new ToaDo(1, huongBdo.X, huongBdo.Y, Hkm, Vvt, "abc");
            ToaDo td2 = new ToaDo(2, huongGocT.X, huongGocT.Y, Hkm, Vvt, "abc");

            qd = new List<ToaDo>();
            qd.Add(td1);
            qd.Add(td2);
            //MucTieu mb = new MucTieu("Quân sự (địch)", 5, qd, Color.Red);
            
            button4.Enabled = true;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            mb = new MucTieu("Quân sự (địch)", 5, qd, Color.Red);
        }

        private void button8_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            numericUpDown2.Value = Convert.ToDecimal(7);
        }

        private void button50_Click(object sender, EventArgs e)
        {

        }
    }
}
